<template>
  <div>
    <navigation-bar>
      <div class="searchContent">
        <el-input v-model="examineeName" placeholder="请输入名称" size="mini"
          style="width: 150px; margin-right: 10px"></el-input>
        <custom-button type="search" style="margin-right: 10px" @searchClick="searchClick"></custom-button>
        <custom-button type="reset" style="margin-right: 10px" @resetClick="resetClick"></custom-button>
        <custom-button type="custom" title="入组" style="margin-right: 10px" @customClick="customClick"></custom-button>
        <custom-button type="custom" title="确认入组" style="margin-right: 10px" @customClick="customClick"></custom-button>
        <!-- <custom-button type="custom" title="未入组" style="margin-right: 10px"
                       @customClick="remove"></custom-button> -->
        <el-button :disabled="showRemove" size="mini" tyle="margin-right: 10px" type="primary"
          @click="remove">未入组</el-button>
        <!-- <custom-button type="add" title="备选" 	@customClick="customClick"></custom-button> -->
      </div>
    </navigation-bar>
    <table-index :dataSource="dataSource" :tableColumns="tableColumns" :btnList="btnList" :pageSize="pageSize"
      :pageCount="pageCount" :pageNo="pageNo" @pageChange="pageChange" selection
      @handleSelectionChange="handleSelectionChange"></table-index>

    <div v-if="dialogShow">
      <custom-dialog :title="dialogTitle" size="large" @dialogClose="dialogClose">
        <form-index :formConfig="formConfig" @submit="submit" :type="type" ref="form">
          <!--  <div>这里还可以插入其它内容进行拓展</div> -->
        </form-index>
      </custom-dialog>
    </div>
    <el-dialog title="入组人数" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="入组人数" :label-width="formLabelWidth">
          <el-input v-model="form.num" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClilk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { groupingOperationForm } from "@/assets/form/managementCenter/subjectManagement/firstCheckInManagement/groupingOperationForm"
import { ungroupedForm } from "@/assets/form/managementCenter/subjectManagement/firstCheckInManagement/ungroupedfiledForm"
import customDialog from "@/components/customComponents/customDialog"
import tableIndex from "@/components/customComponents/customeTable/tableIndex"
import customButton from "@/components/customComponents/customForm/customButton"
import formIndex from "@/components/customComponents/customForm/formIndex"
import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
import { AjaxCommon } from "@/libs/apiCommon"
import { PopupCommon } from "@/libs/popupCommon"
import { SessionCommon } from "@/libs/sessionCommon"
export default {
  components: {
    navigationBar,
    customButton,
    tableIndex,
    customDialog,
    formIndex,
  },
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {
        num: '',
      },
      showRemove: true,
      // 入组1，确认入组2
      cusType: 1,
      groupData: [],
      leftSelectIds: [],
      examineeName: "",
      // table 相关参数  begin
      dataSource: [],
      tableColumns: [
        {
          prop: "groupInStatus",
          label: "入组状态",
          dictName: "group_in_status",
        },
        {
          prop: "groupName",
          label: "分组名称",
        },
        {
          prop: "createTime",
          label: "入组时间",
          width: 160,
        },
        {
          prop: "experimentBeginTime",
          label: "试验开始时间",
          width: 160,
        },
        {
          prop: "filterNo",
          label: "筛选号",
        },
        {
          prop: "randomNo",
          label: "随机号",
        },
        {
          prop: "groupSeq",
          width: 120,
          label: "入组顺序号",
        },
        {
          prop: "drugSeq",
          width: 120,
          label: "给药顺序号",
        },
        {
          prop: "examineeName",
          label: "姓名",
        },
        {
          prop: "examineeEn",
          label: "姓名缩写",
        },
        {
          prop: "sex",
          label: "性别",
          dictName: "sex",
        },
      ],
      btnList: [],
      pageSize: 20,
      pageCount: 100,
      pageNo: 1,
      // table 相关参数  end

      // 弹窗是否显示
      dialogShow: false,
      // 弹窗标题 (新增修改不同)
      dialogTitle: "",

      // form的配置信息
      formConfig: [],
      type: "add",
    }
  },

  methods: {
    handleSelectionChange(d) {
      this.leftSelectIds = d
      if (this.leftSelectIds.length > 0) {
        this.showRemove = false
      } else if (this.leftSelectIds.length == 0) {
        this.showRemove = true
      }
    },
    submit(d) {
      let self = this
      console.log(self, d)
      if (this.cusType == 1) {
        // 入组
        // PopupCommon.confirm("确认入组")
        // 	.then(() => {
        AjaxCommon(
          "/ccdc/pm/em/in/emInGroup/inGroup",
          "post",
          d,
          true
        )
          .then((result) => {
            if (result.data.code == 200) {
              PopupCommon.notify("success", result.data.message)
              this.searchClick()
            } else {
              PopupCommon.notify("error", result.data.message)
            }
          })
          .catch((e) => {
            PopupCommon.notify("error", e.message)
          })
        // })
        // .catch((err) => {
        // 	self.dialogShow = false
        // 	console.log(err)
        // })
      } else if (this.cusType == 3) {
        // let self = this
        let ids = []
        this.leftSelectIds.forEach(e => {
          ids.push(e.id)
        });
        AjaxCommon(
          "/ccdc/pm/em/in/emInGroup/excludeOutGroupBatch",
          "post",
          {
            idList: ids,
            reason: d.reason + ""
          },
          true
        ).then((res) => {
          if (res.data.code == 200) {
            PopupCommon.notify("success", res.data.message)
            self.initPage()
            self.dialogClose()
          } else {
            PopupCommon.notify("error", res.data.message)
          }
        })
      }

    },
    //入组弹框
    addClilk() {
      let self = this
      let num = Number(this.form.num)
      AjaxCommon(
        "/ccdc/pm/em/in/emInGroup/autoInGroup",
        "post", { num }
      ).then((result) => {
        if (result.data.code == 200) {
          PopupCommon.notify("success", result.data.message)
          self.initPage()
          self.dialogClose()
        } else {
          PopupCommon.notify("error", result.data.message)
        }
      })
        .catch((e) => {
          console.log(e, 'e.message');
        })

      this.dialogFormVisible = false
    },
    //入组前查询人数
    queryPro() {
      let projectTestperiod =
        window.sessionStorage.getItem("projectTestperiod")
      projectTestperiod = JSON.parse(projectTestperiod)
      console.log(projectTestperiod);
      AjaxCommon(
        "/ccdc/cfg/pm/pmExp/queryById?id=" + projectTestperiod.testId,
        "get",
      ).then((result) => {
        if (result.data.code == 200) {
          this.form.num = result.data.result.planSeries
          // PopupCommon.notify("success", result.data.message)
        } else {
          PopupCommon.notify("error", result.data.message)
        }
      })
        .catch((e) => {
          console.log(e, 'e.message');
          // PopupCommon.notify("error", e.message)
        })
    },
    customClick(d) {
      let self = this
      if (d == "入组") {
        this.dialogFormVisible = true
        this.queryPro()
        // PopupCommon.confirm("确认入组操作").then(() => {
        //   AjaxCommon(
        //       "/ccdc/pm/em/in/emInGroup/autoInGroup",
        //       "post"
        //   ).then((result) => {
        //     if (result.data.code == 200) {
        //       PopupCommon.notify("success", result.data.message)
        //     } else {
        //       PopupCommon.notify("error", result.data.message)
        //     }
        //   })
        //     .catch((e) => {
        //         console.log(e,'e.message');
        //         // PopupCommon.notify("error", e.message)
        //       })
        // })
        // if (this.leftSelectIds.length != 1) {
        // 	PopupCommon.notify("info", "请选择一个受试者！")
        // 	return
        // }
        // this.cusType = 1
        // let formConfigItem = this.formConfig
        // formConfigItem.map((t) => {
        // 	Object.keys(t).map((res) => {
        // 		if (res == "fieldName") {
        // 			if (t[res] == "examineeCode") {
        // 				t.filedValue = this.leftSelectIds[0].examineeCode
        // 			} else if (t[res] == "examineeName") {
        // 				t.filedValue = this.leftSelectIds[0].examineeName
        // 			} else if (t[res] == "filterNo") {
        // 				t.filedValue = this.leftSelectIds[0].filterNo
        // 			} else if (t[res] == "randomNo") {
        // 				t.filedValue = this.leftSelectIds[0].randomNo
        // 			} else if (t[res] == "groupSeq") {
        // 				t.filedValue = this.leftSelectIds[0].groupSeq
        // 			} else {
        // 				t.filedValue = ""
        // 			}
        // 		}
        // 	})
        // })
        // formConfigItem.groupId = this.leftSelectIds[0].id
        // this.formConfig = formConfigItem
        // this.type = "add"
        // this.dialogShow = true
        // this.dialogTitle =
        // 	"新增" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
      } else {
        this.cusType = 2
        AjaxCommon("/ccdc/pm/em/in/emInGroup/getTip", "get", {}, true).then(
          (res) => {
            PopupCommon.confirm(res.data.message).then(() => {
              AjaxCommon(
                "/ccdc/pm/em/in/emInGroup/confirmInGroup",
                "post",
                {},
                true
              ).then((res1) => {
                if (res1.data.code == 200) {
                  PopupCommon.notify("success", res1.data.message)
                  self.initPage()
                } else {
                  PopupCommon.notify("error", res1.data.message)
                }
              })
            })
              .catch((err) => {
                console.log(err.message);
                // PopupCommon.notify("error", err.message)
              })
          })
      }

    },
    dialogClose() {
      this.dialogShow = false
    },
    searchClick() {
      this.pageNo = 1
      this.initPage()
    },
    resetClick() {
      this.examineeName = ""
    },
    remove() {
      let self = this
      var a = 0
      this.leftSelectIds.forEach(e => {
        if ("DONE" === e.groupInStatus){
          a = 1
        }
      });
      if (1 === a){
        PopupCommon.notify("info", "仅限操作入组状态为[待入组]行数据")
        return
      }
      console.log('1111', this.formConfig);
      // this.showTepe = "Show",
      this.cusType = 3
      this.type = "ungrouped"
      this.dialogShow = true
      this.dialogTitle =
        "未" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
      self.formConfig = ungroupedForm
      // let self = this
      // AjaxCommon(
      //     "/ccdc/businessSelect/excludeOutGroup",
      //     "post",
      //     {},
      //     true
      // ).then((res) => {
      //   if (res.data.code == 200) {
      //     self.groupData = res.data.result
      //     self.formConfig = groupingOperationForm(res.data.result)
      //   } else {
      //     PopupCommon.notify("error", res.data.message)
      //   }
      // })
    },
    // 分页点击事件
    pageChange(p) {
      this.pageNo = p
      this.initPage()
    },
    // 初始化事件
    initPage() {
      let self = this
      AjaxCommon(
        "/ccdc/pm/em/in/emInGroup/listByPage",
        "post",
        {
          examineeName: this.examineeName,
          pageNo: this.pageNo,
          pageSize: this.pageSize,
        },
        true
      ).then((res) => {
        if (res.data.code == 200) {
          self.dataSource = res.data.result.records
          self.pageCount = res.data.result.total
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },

    getGroup() {
      let self = this
      AjaxCommon(
        "/ccdc/businessSelect/expGroupSelect",
        "post",
        {},
        true
      ).then((res) => {
        if (res.data.code == 200) {
          self.groupData = res.data.result
          self.formConfig = groupingOperationForm(res.data.result)
        } else {
          PopupCommon.notify("error", res.data.message)
        }
      })
    },
  },
  mounted() {
    this.initPage()
    this.getGroup()
  },
}
</script>

<style></style>
